package day04;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/4/11
 */
public class BinarySearch {
    public static int binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            int mid = (left + right) / 2;

            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return -1;
    }

    public static void main(String[] args) {
            int[] arr = {5,8,13,20,25,30,45,50};
            System.out.println(binarySearch(arr, 20));
    }
}
